package org.example.hansp.recursion;

public class BinarySearch {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int target = 5;

        int result = binarySearch(array, target, 0, array.length - 1);
        if (result != -1) {
            System.out.println("元素 " + target + " 在数组中的索引是: " + result);
        } else {
            System.out.println("数组中没有找到元素 " + target);
        }

    }
    public static int binarySearch(int[] arr, int target,int left, int right) {

        if(left > right){
            return -1;
        }
        int mid = (left + right) / 2;
        if (arr[mid] == target) {
            return mid;
        }
        if (arr[mid] > target) {
            return binarySearch(arr, target, left, mid - 1);
        }
        return binarySearch(arr, target, mid + 1, right);
    }
}
